#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"

/* 
	JSON对象数组({ "key" : [{"key1": "value1"},{"key2": "value2"}]})
	{
		"my" : 
		[
			{"key": "i love you"},
			{"key1": "forever"}
		]
	}
*/
// int main()
// {
// 	cJSON *root, *item_1, *subobject_1, *subobject_2;

// 	/*组合json*/
// 	root = cJSON_CreateObject(); //创建根数据对象
// 	item_1 = cJSON_CreateArray();
// 	subobject_1 = cJSON_CreateObject();
// 	subobject_2 = cJSON_CreateObject();

// 	cJSON_AddStringToObject(subobject_1, "key", "i love you");
// 	cJSON_AddStringToObject(subobject_2, "key1", "forever");
// 	cJSON_AddItemToArray(item_1, subobject_1);
// 	cJSON_AddItemToArray(item_1, subobject_2);
// 	cJSON_AddItemToObject(root, "my", item_1);

// 	char *outprint_root = cJSON_Print(root);
// 	printf("outprint_root = \r\n%s\r\n", outprint_root);

// 	/*解析json*/

// 	cJSON_Delete(root); //删除结构体，释放内存
// 	cJSON_free(outprint_root);
// 	outprint_root = NULL; //防止这玩意成为野指针
// }



/* 
	JSON数组({ "key" : [value]})
	{
		"key" : [ 520, 1314 ],
		"key1" : [ 520, 3344 ]
	}
*/
// int main()
// {
// 	cJSON *root, *item_1, *item_2;
// 	int Array_1[2] = {520, 1314};
// 	int Array_2[2] = {520, 3344};
// 	root = cJSON_CreateObject();		//创建根数据对象
// 	item_1 = cJSON_CreateIntArray(Array_1, 2);
// 	item_2 = cJSON_CreateIntArray(Array_2, 2);
// 	cJSON_AddItemToObject(root, "Key", item_1);
// 	cJSON_AddItemToObject(root, "Key1", item_2);

// 	char *outprint_root = cJSON_Print(root);
// 	printf("outprint_root = \r\n%s\r\n", outprint_root);

// 	cJSON_Delete(root); //删除结构体，释放内存
// 	cJSON_free(outprint_root);
// 	outprint_root = NULL; //防止这玩意成为野指针
// }



/* 
	JSON对象({ "key" : {value}})
	{
		"key" : {"520" : "i love you"},
		"key1":{"1314" : "forever"}
	}
*/
// int main()
// {
// 	cJSON *root, *subobject_1, *subobject_2;

// 	/* 组合json */
// 	root = cJSON_CreateObject();  //创建根数据对象
// 	subobject_1 = cJSON_CreateObject(); //创建根数据对象
// 	subobject_2 = cJSON_CreateObject(); //创建根数据对象
// 	cJSON_AddStringToObject(subobject_1, "520", "i love you");
// 	cJSON_AddStringToObject(subobject_2, "1314", "forever");
// 	cJSON_AddItemToObject(root, "key", subobject_1);
// 	cJSON_AddItemToObject(root, "key", subobject_2);

// 	char *outprint_root = cJSON_Print(root);
// 	printf("outprint_root = \r\n%s\r\n", outprint_root);

// 	/* 解析json */


// 	cJSON_Delete(root); //删除结构体，释放内存
// 	cJSON_free(outprint_root);
// 	outprint_root = NULL; //防止这玩意成为野指针
// }
 



int main()
{
	cJSON *root;
	cJSON *_json,*_json_name,*_json_passwd,*_json_num;
	char json_name[10];
	char json_passwd[10];
	unsigned char json_num;
	/* 创建json */
	root  =  cJSON_CreateObject();   //创建根数据对象
	cJSON_AddStringToObject(root,"name","fengxin");  //加入键值，加字符串
	cJSON_AddStringToObject(root,"passwd","123");
	cJSON_AddNumberToObject(root,"num",1);  //加整数

	char *outprint_root = cJSON_Print(root); //将json形式打印成正常字符串形式
	printf("%s\n", outprint_root);

	// outprint_root = cJSON_PrintUnformatted(root);
	// printf("%s\n", outprint_root);
	
	/* 解析json */
	_json = cJSON_Parse(outprint_root);				   //解析成json形式
	_json_name = cJSON_GetObjectItem( _json , "name" );  //获取键值内容
	_json_passwd = cJSON_GetObjectItem(_json , "passwd" );
	_json_num = cJSON_GetObjectItem( _json , "num" );
	strcpy(json_name, _json_name->valuestring);
	strcpy(json_passwd, _json_passwd->valuestring);
	json_num = _json_num->valueint;
	printf("name:%s,passwd:%s,num:%d\n", _json_name->valuestring, _json_passwd->valuestring, _json_num->valueint);
	printf("name:%s,passwd:%s,num:%d\n", json_name, json_passwd, json_num);

	// 释放内存
	cJSON_Delete(root);
	cJSON_Delete(_json); //释放内存
	cJSON_free(outprint_root);
	outprint_root = NULL; //防止这玩意成为野指针

	return 0;
}
